import Vue from "vue";
import Vuex from "vuex";
import * as api from "@/api/permission"

import allRoutes from "@/router/generateRoutes"

import recursionRoutes from "@/router/recursionRoutes"

import router from "@/utils/routerGuard"
let routes = allRoutes.find(route => route.name === "home")

import dynamicRoutes from '@/router/dynamicRoutes' 

Vue.use(Vuex);

export default {
  namespaced: true,
  state: {
    userMenu:[],
    crumb:[]
  },
  getters: {},
  mutations: {
    SET_USER_MENU(state,payload){
      state.userMenu = [...payload]
      dynamicRoutes.children = [...dynamicRoutes.children,...payload]
      router.addRoute(dynamicRoutes)
    },
    SET_CRUMB(state,payload){
      state.crumb = payload
    }
  },
  actions: {
    FETCH_MENU_LIST({commit}){
      return api.getMenuListApi()
      .then(res => {
        let arr = recursionRoutes(res.data.menuList,routes.children)
        commit('SET_USER_MENU',arr)
      })
    }
  },
  modules: {},
};
